package com.hadwinling.controller;

import com.hadwinling.service.IoTDBService;
import com.hadwinling.service.impl.IoTDBServiceImpl;
import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.rpc.StatementExecutionException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
public class IotDBController {

    @Resource
    private IoTDBService iotdbService;

    @GetMapping("/")
    public String index(){
        return "index";
    }

    /**
     * http://localhost:8080/set/root.dc_dx1
     * 设置存储组
     * @param storage
     * @return
     */
    @GetMapping("/set")
    public String setStorageGroup(@RequestParam(name = "storage") String storage) {
        System.out.println("开始创建：" + storage);
        iotdbService.setStorageGroup(storage);
        return "index";
    }

    /**
     * 删除存储组
     * @param storage
     * @return
     */
    @GetMapping("/delete")
    public String deleteStorageGroup(@RequestParam(value = "storage") String storage) {
        System.out.println("开始删除：" + storage);
        iotdbService.deleteStorageGroup(storage);
        return "index";
    }
    /**
     *  用法 ：/insert/root.dc.dx01/52/6
     * @param storage
     * @param zujianNum 组件的数量
     * @param zhibiaoNum    指标的数量 设备的数量
     * @return
     * @throws StatementExecutionException
     * @throws IoTDBConnectionException
     */
    @PostMapping("/insert")
    @ResponseBody
    public String insert(@RequestParam(value = "storage") String storage,
                         @RequestParam(value = "zujianNum")int zujianNum,
                         @RequestParam(value = "zhibiaoNum") int zhibiaoNum) throws IOException {
        long start = System.currentTimeMillis();
        iotdbService.insert(storage, zujianNum, zhibiaoNum);
        long end = System.currentTimeMillis();
        System.out.println("花费："+(end-start)+"豪秒");
        return "花费："+(end-start)/1000.0+"秒";
    }

    /**
     *  用法 ：/insert/root.dc.dx01/52/6
     * @param storage
     * @param zujianNum 组件的数量
     * @param zhibiaoNum    指标的数量 设备的数量
     * @return
     * @throws StatementExecutionException
     * @throws IoTDBConnectionException
     */
    @PostMapping("/insertWithRHB")
    @ResponseBody
    public String insertWithRHB(@RequestParam(value = "storage") String storage,
                         @RequestParam(value = "zujianNum")int zujianNum,
                         @RequestParam(value = "zhibiaoNum") int zhibiaoNum) throws IOException {
        long start = System.currentTimeMillis();
        iotdbService.insertWithRHB(storage, zujianNum, zhibiaoNum);
        long end = System.currentTimeMillis();
        System.out.println("花费："+(end-start)+"豪秒");
        return "花费："+(end-start)/1000.0+"秒";
    }

}
